package com.atmilan.quartz.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atmilan.quartz.mapper.SupplierMapper;
import com.atmilan.quartz.model.dto.buyer.SupplierDTO;
import com.atmilan.quartz.model.entity.buyer.Supplier;
import com.atmilan.quartz.service.ISupplierService;
import com.atmilan.quartz.utils.BeanConvert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;

/**
 * 供应商Service业务层处理
 * 
 * @author muyu
 * @date 2020-09-01
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    // 10秒缓存, 加本地缓存,降低db压力
    TimedCache<Long, SupplierDTO> timedCache = CacheUtil.newTimedCache(60 * 1000);

    /**
     * 查询供应商
     * 
     * @param id 供应商ID
     * @return 供应商
     */
    @Override
    public SupplierDTO getSupplierById(Long id) {
        SupplierDTO dto = timedCache.get(id);
        if (dto == null) {
            dto = BeanConvert.copyBean(supplierMapper.selectByPrimaryKey(id), SupplierDTO.class);
            if (dto != null) {
                timedCache.put(id, dto);
            }
        }
        return dto;
    }

}
